package cn.ycbjie.ycaudioplayer.newpack.linkagescroll.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

import cn.ycbjie.ycaudioplayer.R;
import cn.ycbjie.ycaudioplayer.newpack.linkagescroll.mybeans.MyPlayerlistBean;

/**
 * Created by GUO on 2015/12/3.
 */

/**
 * Created by Nelson on 15/11/9.
 */
public class RecyclerBaseAdapter3 extends RecyclerView.Adapter<RecyclerBaseAdapter3.VH> {

    private final static String TAG = "RecyclerBaseAdapter";

    private List<MyPlayerlistBean> itemDataList = null;
    private Context context = null;

    private int defItem = -1;

    public RecyclerBaseAdapter3(Context context, List<MyPlayerlistBean> itemDataList) {
        this.itemDataList = itemDataList;
        this.context = context;
    }
    public void setDefSelect(int position) {
        this.defItem = position;
        notifyDataSetChanged();
    }

//    private OnItemClickListener mOnItemClickListener;//声明接口
//
//    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
//        mOnItemClickListener = onItemClickListener;
//    }


    //② 创建ViewHolder
    public static class VH extends RecyclerView.ViewHolder{
        public final TextView title;
        public final TextView playid;
        public final TextView playpic;
        public final TextView playshu;
        public final ImageView img_playerlist_shiting;
        public VH(View v) {
            super(v);
            title = (TextView) v.findViewById(R.id.tv_playerlist_title);
            playid = (TextView)v.findViewById(R.id.tv_playerlist_id);
            playpic=(TextView)v.findViewById(R.id.tv_playerlist_pic);
            playshu=(TextView)v.findViewById(R.id.tv_playerlist_shu);
            img_playerlist_shiting = (ImageView)v.findViewById(R.id.img_playerlist_shiting);
        }
    }


//    @Override
//    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
//                                                      int viewType) {
//        TextView title;
//        View v = LayoutInflater.from(context).inflate(R.layout.list_item_layout2, parent, false);
//        final RecyclerView.ViewHolder holder = new RecyclerItemViewHolder(context, v);
//        holder.title
//        holder.title = (TextView) v.findViewById(R.id.tv_playerlist_title);
//        return  holder;
//    }
////    class ListViewHolder extends RecyclerView.ViewHolder {
////        TextView title;
////
////        public ListViewHolder(View itemView) {
////            super(itemView);
////            title = (TextView) itemView.findViewById(R.id.tv_playerlist_title);
////        }
////    }

    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout, parent, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(final VH holder, int position) {

        if (defItem == position) {
            holder.title.setTextColor(Color.parseColor("#FF0000"));

        }
        else
        {
            holder.title.setTextColor(Color.parseColor("#000000"));
        }
        if(itemDataList.get(position).isTrial())
        {
            holder.img_playerlist_shiting.setVisibility(View.VISIBLE);
        }

        holder.title.setText(itemDataList.get(position).getTitle());
        holder.playid.setText(itemDataList.get(position).getId());
        holder.playpic.setText((itemDataList.get(position).getCover()));
        holder.playshu.setText(position+".>");
//        holder.title.setText(itemDataList.get(position).getTitle());
        //判断是否设置了监听器
        if(mOnItemClickListener != null){
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition(); // 1
                    mOnItemClickListener.onItemClick(holder.itemView,position); // 2
                }
            });
        }
        if(mOnItemLongClickListener != null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(holder.itemView,position);
                    //返回true 表示消耗了事件 事件不会继续传递
                    return true;
                }
            });
        }





//        View itemView = null;
//        try {
//            itemView = ((RelativeLayout) holder.itemView).getChildAt(0);
//       }
//       catch (Exception ex)
//       {
//           ex.printStackTrace();
//       }
//
//        if (mOnItemClickListener != null) {
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    int position = holder.getLayoutPosition();
//                    mOnItemClickListener.onItemClick(holder.itemView, position);
//                }
//            });
//        }
//
//
    }

    @Override
    public int getItemCount() {
       return itemDataList.size();
    }


    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    public void setListData(List<MyPlayerlistBean> data) {
        itemDataList = data;
        notifyDataSetChanged();
    }

    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }

    public interface OnItemLongClickListener{
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }


}
